package cn.wolfcode.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 数据管理之字典目录
 */
@Api(value = "字典目录",description = "字典目录资源控制器")
@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {
    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequestMapping("/list")
    @RequiresPermissions(value = {"systemDictionary:list","数据字典字典列表"})
    public String list(Model model, QueryObject qo){
        PageInfo<SystemDictionary> pageInfo = systemDictionaryService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "systemDictionary/list";
    }
    // 处理删除请求
    @GetMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            systemDictionaryService.delete(id);
        }
        return "redirect:/systemDictionary/list.do"; // 让浏览器重新发一次请求
    }
    // 去新增或去修改页面
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if (id != null) { // 表示去修改
            SystemDictionary systemDictionary = systemDictionaryService.get(id);
            model.addAttribute("systemDictionary", systemDictionary);
        }
        return "systemDictionary/input"; // 让浏览器重新发一次请求
    }
    // 新增或修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary){
        JsonResult jr = new JsonResult();
        try {
            if(systemDictionary.getId() == null){ // 新增
                systemDictionaryService.save(systemDictionary);
            }else {
                systemDictionaryService.update(systemDictionary); // 修改
            }
            jr.setFlag(true);
            jr.setMsg("保存成功");
        } catch (Exception e) {
            jr.setFlag(false);
            jr.setMsg("保存失败");
        }
        return jr;
    }
}

